package com.wny;
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class SocketsClient {
	
	private String serverID;
	private int serverPort = 8888;
	private Socket mySocket;
	
	public SocketsClient(String serverName) throws UnknownHostException, IOException
	{
		serverID = serverName;
		mySocket = new Socket(serverID, serverPort);
	}
	//sends data to the server and returns server response
	public char[] sendData(byte[] toSend) throws IOException
	{
		char[] recieved = new char[2000];

		OutputStream out = mySocket.getOutputStream();
		InputStream in = mySocket.getInputStream();
		InputStreamReader inRead = new InputStreamReader(in);
		BufferedReader buffRead = new BufferedReader(inRead);
		out.write(toSend);
		buffRead.read(recieved);
		return recieved;
	}
	public void sendNonblocking(byte[] toSend) throws IOException
	{
		OutputStream out = mySocket.getOutputStream();
		out.write(toSend);
	}
	
	//closes connection
	public void closeConnection() throws IOException
	{

		mySocket.close();
	}
	
}
